package com.dd.admin.business.category.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dd.admin.common.aop.operationLog.aop.OperLog;
import com.dd.admin.common.aop.operationLog.aop.OperType;
import com.dd.admin.common.security.SecurityUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.dd.admin.common.model.UpdateGroup;
import com.dd.admin.common.model.result.ResultBean;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotBlank;
import com.dd.admin.business.category.entity.Category;
import com.dd.admin.business.category.domain.CategoryVo;
import com.dd.admin.business.category.domain.CategoryDto;
import com.dd.admin.business.category.service.CategoryService;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 分类 前端控制器
 * </p>
 *
 * @author 727869402@qq.com
 * @since 2024-05-22
 */
@Api(tags = "分类")
@RestController
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @ApiOperation(value = "分类-分页列表")
    @ApiOperationSupport(order = 1)
    @GetMapping("/admin/category/page")
    @OperLog(operModule = "获取分页列表",operType = OperType.QUERY,operDesc = "获取分页列表")
    public ResultBean<IPage<CategoryVo>> page(CategoryDto categoryDto) {
        categoryDto.setShopId(SecurityUtil.getLoginUser().getDeptId());
        IPage<CategoryVo> pageInfo = categoryService.selectCategoryPage(categoryDto);
        return ResultBean.success(pageInfo);
    }

    @ApiOperation(value = "分类-列表")
    @ApiOperationSupport(order = 2)
    @GetMapping("/admin/category/list")
    public ResultBean<List<CategoryVo>> list(CategoryDto categoryDto) {
        categoryDto.setShopId(SecurityUtil.getLoginUser().getDeptId());
        List<CategoryVo> list = categoryService.selectCategoryList(categoryDto);
        return ResultBean.success(list);
    }

    @ApiOperation(value = "分类-添加")
    @ApiOperationSupport(order = 3)
    @PostMapping("/admin/category/add")
    @OperLog(operModule = "添加分类",operType = OperType.ADD,operDesc = "添加分类")
    public ResultBean<Category> add(@RequestBody @Validated CategoryDto categoryDto) {
        Category category = BeanUtil.copyProperties(categoryDto, Category.class);
        categoryService.save(category);
        return ResultBean.success(category);
    }

    @ApiOperation(value = "分类-查询")
    @ApiOperationSupport(order = 4)
    @GetMapping("/admin/category/{categoryId}")
    public ResultBean<CategoryVo> get(@PathVariable @NotBlank String categoryId) {
        Category category = categoryService.getById(categoryId);
        CategoryVo categoryVo = BeanUtil.copyProperties(category,CategoryVo.class);
        return ResultBean.success(categoryVo);
    }

    @ApiOperation(value = "分类-修改")
    @ApiOperationSupport(order = 5)
    @PostMapping("/admin/category/update")
    public ResultBean<Category> update(@RequestBody @Validated(UpdateGroup.class) CategoryDto categoryDto) {
        Category category = BeanUtil.copyProperties(categoryDto, Category.class);
        categoryService.updateById(category);
        return ResultBean.success(category);
    }

    @ApiOperation(value = "分类-删除")
    @ApiOperationSupport(order = 6)
    @GetMapping("/admin/category/delete/{categoryId}")
    public ResultBean<Category> delete(@PathVariable @NotBlank String categoryId) {
        Boolean b = categoryService.removeById(categoryId);
        return ResultBean.success(b);
    }
}
